package dao;
import java.sql.*;
import java.util.*;

public class UserDao {
	
	private Connection con;
	 
	public UserDao()
	{
		
		try{
			con=DBPool.getConnection();
			
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		
	}
	public Vector<String> getUserByRoomId(int roomid)
	{
		Vector<String> userlist=new Vector<String>();
		String sql="select * from user where roomid="+roomid;
		Statement smt=null;
		ResultSet rs=null;
		try{
			smt=con.createStatement();
			rs=smt.executeQuery(sql);
			while(rs.next())
			{
				userlist.add(rs.getString(2));
			}
			rs.close();
			smt.close();
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return userlist;
	}
	public void setUserRoomId(String username,int roomid)
	{
		Statement smt=null;
		String sql="update user set roomid="+roomid+" where username="+"'"+username+"'";
		try{
			smt=con.createStatement();
			smt.executeUpdate(sql);
		    smt.close();
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
	}
	public int getUserId(String username)
	{
		int id=0;
		String sql="select * from user where username="+"'"+username+"'";
		Statement smt=null;
		ResultSet rs=null;
		try{
			smt=con.createStatement();
			rs=smt.executeQuery(sql);
			rs.next();
			id=rs.getInt(1);
			rs.close();
			smt.close();
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return id;
	}
	public  int isRegister(String username,String password)
	{
		int reg=0;
		String sql="select * from user where username="+"'"+username+"'";
		Statement smt=null;
		ResultSet rs=null;
		try{
			smt=con.createStatement();
			rs=smt.executeQuery(sql);
			if(!rs.next())
				reg=0;//没有此用户
			else
			{
				if(password.equals(rs.getString(3)))
					reg=2;//用户合法
				else
					reg=1;//用户密码不正确
			}
			rs.close();
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return reg;
	}
}